package com.example.admin.controller;

import com.example.admin.service.BlogService;

import com.example.xo.pojo.Blog;
import com.example.xo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 992c
 */
@RestController
@Slf4j
@RequestMapping("blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping("add")
    public Result add(@RequestBody Blog blog) {
        meod(blog);
        return Result.success(blogService.add(blog));
    }

    @PutMapping("update")
    public Result update(@RequestBody Blog blog) {
        meod(blog);
        return Result.success(blogService.update(blog));
    }

    private static Result meod(Blog blog) {

        if (blog.getTitle() == null || blog.getTitle().trim().length() == 0) {
            log.info("标题不能为空");
            return Result.error();
        }
        if (blog.getTitle().length() > 50) {
            log.info("标题长度过长");
            return Result.error();
        }

        if (blog.getAuthor().length() > 10) {
            log.info("作者名字过长");
            return Result.error();
        }
        if (blog.getContent() == null || blog.getContent().trim().length() == 0) {
            log.info("正文内容不能为空");
            return Result.error();
        }
        if (blog.getAuthor() == null || blog.getAuthor().trim().length() == 0) {
            log.info("作者名字不能为空");
            return Result.error();
        }
        return Result.error();
    }

    @GetMapping("")
    public Result findAll(){

        return  Result.success(blogService.findAll());
    }
    @GetMapping("findPoById/{id}")
    public Result findById(@PathVariable String id){

        return Result.success(blogService.findPoById(id));
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id){

        return  Result.success(blogService.deleteById(id));
    }
}
